package com.ailargemodel.job;

import com.ailargemodel.pojo.User;
import com.ailargemodel.service.UserService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.data.redis.core.ValueOperations;
import org.springframework.scheduling.annotation.Scheduled;
import org.springframework.stereotype.Component;


import java.util.Arrays;
import java.util.List;
import java.util.concurrent.TimeUnit;
import java.util.stream.Collectors;

/**
 * 用于 缓存预热
 */
@Slf4j
@Component
public class PreCacheJob {
    @Autowired
    private UserService userService;

//    白名单用户, 或者 重点用户
    private List<User> mainUser = Arrays.asList(new User("51202224102"));

    @Autowired
    private RedisTemplate<String, Object> redisTemplate;

    // 每晚 2：00 更新系統
    @Scheduled(cron = "0 0 2 * * *")
    public void doCacheRecomendHistroy() {
//        String studyId = mainUser.get(0).getStudyId();
        List<String> importUserStudyIds = mainUser.stream().map(User::getStudyId)
                .collect(Collectors.toList());

        for (String studyId : importUserStudyIds) {
            String redisKey = String.format("AIassistant::AIfuction::"+studyId);
            ValueOperations<String, Object> valueoperations = redisTemplate.opsForValue();

            try {
                valueoperations.set(redisKey,"this is my test data",1, TimeUnit.DAYS);
            } catch (Exception e) {
                log.error("redis cache is error",e );
            }
        }

    }
}
